In [1]:
%%writefile myfile.txt
Hello this is a text file
this is the second line
this is the third line


Writing myfile.txt

In [2]:
myfile = open('myfile.txt')

In [3]:
myfile = open('woopsmy.txt')


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-3-b45f4538e5ce> in <module>()
----> 1 myfile = open('woopsmy.txt')

FileNotFoundError: [Errno 2] No such file or directory: 'woopsmy.txt'

In [4]:
file = open('myfile.txt')

In [5]:
pwd


Out[5]:
'/home/russia/Documents/Notebooks/py_udemy'

In [6]:
ls


Booleans.ipynb               russ_first_notebook.ipynb
Dictionaries.ipynb           Sets.ipynb
Floating point values.ipynb  String formatting for printing.ipynb
I O with basic files.ipynb   String Immutability.ipynb
lesson_1.py                  Strings.ipynb
Lists.ipynb                  String - Slicing and Indexing .ipynb
myfile.txt                   Tuples.ipynb
Python Numbers.ipynb         Variable Assignment.ipynb

In [7]:
myfile.read()


Out[7]:
'Hello this is a text file\nthis is the second line\nthis is the third line'

In [9]:
myfile.encoding()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-ffb5d940f9f6> in <module>()
----> 1 myfile.encoding(open(myfile.txt))

AttributeError: '_io.TextIOWrapper' object has no attribute 'txt'

In [13]:
myfile.seek(0) #resets the reader buffer.


Out[13]:
0

In [14]:
myfile.readlines()


Out[14]:
['Hello this is a text file\n',
 'this is the second line\n',
 'this is the third line']

file paths required for different locations:

file = open("C:\Users\Russ\Documents\myfile.txt")

file = open("/Users/Russ/Documents/myfile.txt")

When opening files, they must be closed after use myfile.close() method. Or..............


In [16]:
with open ('myfile.txt') as mynewfile:
    contents = mynewfile.read() #Don't have to worry about closing witht his method.

In [21]:
with open ('myfile.txt',mode='r') as myfile: #in a method bracket '(|' < place the cursor and press shift+tab .ipynb
    contents = myfile.read()

In [22]:
contents


Out[22]:
'Hello this is a text file\nthis is the second line\nthis is the third line'

In [23]:
with open ('myfile.txt',mode='w') as myfile: # in a method bracket '(|' < place the cursor and press shift+tab .ipynb
    contents = myfile.write() # String contents missing, error reported.


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-a25e4cfd6750> in <module>()
      1 with open ('myfile.txt',mode='w') as myfile: #in a method bracket '(|' < place the cursor and press shift+tab .ipynb
----> 2     contents = myfile.write()

TypeError: write() takes exactly one argument (0 given)

In [24]:
with open ('myfile.txt',mode='w') as myfile: # in a method bracket '(|' < place the cursor and press shift+tab .ipynb
    contents = myfile.write('hi') # String contents missing, NO error reported.

In [28]:
with open('myfile.txt',mode='r') as myfile:
    new = myfile.read()
    new

In [29]:
new


Out[29]:
'hi'

In [30]:
%%writefile newone.txt
LINE ONE
LINE TWO
LINE  3


Writing newone.txt

In [32]:
file2 = open('newone.txt')

In [36]:
file2.read()


Out[36]:
'LINE ONE\nLINE TWO\nLINE  3'

In [37]:
file2.seek(0)


Out[37]:
0

In [39]:
with open('newone.txt',mode='r') as f:
    print(f.read())


LINE ONE
LINE TWO
LINE  3

In [42]:
with open('newone.txt',mode='a') as f:
    f.write('\nfourth line')

In [43]:
with open('newone.txt',mode='r') as f:
    print(f.read())


LINE ONE
LINE TWO
LINE  3fourth line
fourth line

In [50]:
with open('jshdgfihwdik.txt',mode='w') as f: # The file is created if non-existent
    f.write('I CREATED THIS FILE USING THE FOLLOWING METHOD\nwith open(\'jshdgfihwdik.txt\',mode=\'w\') as f:\n\tf.write(\'some random text\')')

In [51]:
with open('jshdgfihwdik.txt',mode='r') as f:
    print(f.read())


I CREATED THIS FILE USING THE FOLLOWING METHOD
with open('jshdgfihwdik.txt',mode='w') as f:
	f.write('some random text')

In [ ]: